package com.fare.taluo.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

import com.fare.taluo.util.value.NumberUtils;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils{

	/**
	 * 以下定义了日期的不同格式
	 * 
	 */
	public final static DateFormat DATE_FMT_US_E = new SimpleDateFormat(
			"EEE MMM dd HH:mm:ss z yyyy", Locale.US);
	public final static DateFormat DATE_FMT_US_MDY_HMSA = new SimpleDateFormat(
			"MM/dd/yyyy hh:mm:ss aa", Locale.US);
	public final static DateFormat DATE_FMT_US_MDY_HMA_S = new SimpleDateFormat(
			"MM/dd/yyyy hh:mm aa", Locale.US);
	public final static DateFormat DATE_FMT_US_MDY_HMA = new SimpleDateFormat(
			"MM/dd/yyyy hh:mmaa", Locale.US);
	public final static DateFormat DATE_FMT_US_MDYS_HMA_S = new SimpleDateFormat(
			"MM dd yyyy hh:mm aa", Locale.US);
	public final static DateFormat DATE_FMT_US_MDYS_HMA = new SimpleDateFormat(
			"MM dd yyyy hh:mmaa", Locale.US);

	public final static DateFormat DATE_FMT_CN_YMD = new SimpleDateFormat(
			"yyyy-MM-dd", Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_BARS_YMD_H = new SimpleDateFormat(
			"yyyy-MM-dd HH", Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_BARS_YMD_HM = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm", Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_BARS_YMD_HMS = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_BARS_YMDHMS = new SimpleDateFormat(
			"yyyyMMddHHmmss", Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_BARS_YMD_HMSS = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss.SSS", Locale.CHINA);

	public final static DateFormat DATE_FMT_CN_SLASH_YMD_H = new SimpleDateFormat(
			"yyyy/MM/dd HH", Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_SLASH_YMD_HM = new SimpleDateFormat(
			"yyyy/MM/dd HH:mm", Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_SLASH_YMD_HMS = new SimpleDateFormat(
			"yyyy/MM/dd HH:mm:ss", Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_SLASH_YMD_HMSS = new SimpleDateFormat(
			"yyyy/MM/dd HH:mm:ss.SSS", Locale.CHINA);

	public final static DateFormat DATE_FMT_CN_DOT_YM = new SimpleDateFormat(
			"yyyy.MM", Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_DOT_YMD = new SimpleDateFormat(
			"yyyy.MM.dd", Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_NULL_YMD = new SimpleDateFormat(
			"yyyyMMdd", Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_NULL_Y2MD = new SimpleDateFormat(
			"yyMMdd", Locale.CHINA);
	private static SimpleDateFormat yyyyMMdd = new SimpleDateFormat(
			"yyyy-MM-dd");

	public static String tranferDate(int amount) throws ParseException {
		Calendar calendar = Calendar.getInstance();

		calendar.add(Calendar.DAY_OF_MONTH, amount);
		Date date = calendar.getTime();
		return DATE_FMT_CN_YMD.format(date);
	}

	public static String tranferDate(String beginDate, int amount)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		if (StringUtils.isNotEmpty(beginDate)) {
			calendar.setTime(sdf.parse(beginDate));
		} else {
			calendar.setTime(new Date());
		}
		calendar.add(Calendar.DAY_OF_MONTH, amount);
		Date date = calendar.getTime();
		return DATE_FMT_CN_YMD.format(date);
	}

	/**
	 * 格式化oracle日期
	 * 
	 * @param date
	 * @return
	 */
	public static String formatSqlDay(Date date) {
		String pattern = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		String sqlDate = "to_date('" + format.format(date)
				+ "','yyyy-mm-dd hh24:mi:ss')";
		return sqlDate;
	}

	/**
	 * 
	 * @param str
	 *            FORM=: SimpleDateFormat df = new
	 *            SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	 * @return
	 */
	public static Date toDate(String str) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date date = null;
		try {
			date = df.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 取得两个时间之前的相隔天数
	 * 
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public static int intervalDays(Date beginTime, Date endTime) {
		if (endTime == null || beginTime == null)
			return 0;
		return new Double(
				(endTime.getTime() - beginTime.getTime()) / 3600 / 24 / 1000)
				.intValue();
	}

	/**
	 * 时间差
	 * 
	*/
	public static String intervalTimeStr(Date endTime, Date beginTime){
		Long temp=intervalTime(endTime, beginTime);//相差毫秒数
        long hours = temp / 1000 / 3600;                //相差小时数
        long temp2 = temp % (1000 * 3600);
        long mins = temp2 / 1000 / 60;    
        long temp3 =temp2 %(1000*60);
        long seconds=temp3/1000;
        String timeStr="00:00:00";
        String hourStr="",minStr="",secondStr="";
        if(hours<10l){
        	hourStr="0"+hours;
        }else hourStr=hours+"";
        if(mins<=10l){
        	minStr="0"+mins;
        }else minStr=mins+"";
       if(seconds<=10l){
        	secondStr="0"+seconds;
        }else secondStr=seconds+"";
        timeStr =hourStr+":"+minStr+":"+secondStr;
        return timeStr;
	}
	/**
	 * 返回时间差
	 * 
	 * @param endTime
	 * @param beginTime
	 * @return endTime-beginTime 返回毫秒
	 */
	public static long intervalTime(Date endTime, Date beginTime) {
		if (endTime == null || beginTime == null)
			return 1L;
		return (endTime.getTime() - beginTime.getTime());// 除以1000转换成秒
	}

	// 给定一个日期，返回加减n天后的日期
	/**
	 * 给定一个日期，返回加减n天后的日期 如果是正数则是N天后的日期，如果是负数则，N天前的日期。如果今天是7号-3则是4号
	 */
	public static Date nDaysAfterOneDate(Date basicDate, int n) {
		long nDay = (basicDate.getTime() / (24 * 60 * 60 * 1000) + n)
				* (24 * 60 * 60 * 1000);
		Date result = new Date();
		result.setTime(nDay);
		return result;
	}

	public static List<String> getDatesStr(final String startDay, String endDay) {
		final Date startD = strToDate(startDay + " 00:00", "yyyy-MM-dd hh:mm");
		final Date endD = strToDate(endDay + " 23:59", "yyyy-MM-dd hh:mm");
		Long n = (long) Math
				.ceil(intervalTime(endD, startD) / (24 * 60 * 60.0));
		List list = new ArrayList();
		for (int i = 0; i < n; i++) {
			list.add(dateToStr(nDaysAfterOneDate(startD, (i + 1)), "yyyy-MM-dd"));
		}
		return list;

	}

	public static String getNextDateByDateStr(String datestr, int next,
			String format) throws Exception {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		calendar.setTime(sdf.parse(datestr));
		calendar.add(Calendar.DATE, next);
		return sdf.format(calendar.getTime());
	}

	public static String getNextDateByDateStr(String datestr, String format)
			throws Exception {
		return getNextDateByDateStr(datestr, 1, format);
	}

	public static String nowyyyyMMdd() {
		return yyyyMMdd.format(new Date());
	}

	public static String getDateStr(String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		Date currentTime = new Date();
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	public static String dateToStr(Date d, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String dateString = formatter.format(d);
		return dateString;
	}

	/**
	 * 字符串转成时间
	 * 
	 * @param str
	 * @return
	 */
	public static Date strToDate(String str, String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = df.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * @param java
	 *            .sql.date\
	 * @return 比较两日期大小，yyyy-MM-dd
	 */
	public static boolean complareDate(java.sql.Date d1, java.sql.Date d2) {
		if (d1 == null) {
			return false;
		}
		if (d2 == null) {
			return true;
		}
		if (d1.getTime() > d2.getTime()) {
			return true;
		}

		return false;
	}

	/**
	 * 
	 * @param strTime
	 * @return 返回的时间必须为20130223 8位 ，返回没有小时的时间
	 */
	public static String getPageDateStr(String strTime, String format) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = df.parse(strTime);
			return dateToStr(date, format);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @param strTime
	 * @return 返回的时间必须为20130223 8位 ，返回没有小时的时间
	 */
	public static String getPageDateStr2(String strTime, String format) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		try {
			Date date = df.parse(strTime);
			return dateToStr(date, format);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 得到几天后的时间
	 * 
	 * @param d
	 * @param day
	 * @return
	 */
	public static String getDateAfter(int day) {
		Calendar now = Calendar.getInstance();
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		String pattern = "yyyy-MM-dd";
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(now.getTime());
	}
	public static String getDateAfters(int day,String pattern) {
		Calendar now = Calendar.getInstance();
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		if(pattern ==null)  pattern = "yyyy-MM-dd";
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(now.getTime());
	}

	/**
	 * 得到几小时后的时间
	 * 
	 * @param hour
	 * @return
	 */
	public static String getDateTimeAfter(String dateTimes, int hour) {
		String pattern = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Date afterDates = DateUtils.strToDate(dateTimes, pattern);
		
//		Calendar now = Calendar.getInstance();
//		now.set(Calendar.DATE, now.get(Calendar.HOUR) + hour);
		
		afterDates.setHours(afterDates.getHours()+hour);
		return format.format(afterDates.getTime());
	}

	/**
	 * 得到几天后的时间
	 * 
	 * @param d
	 * @param day
	 * @return "yyyy-MM-dd HH:mm:ss"
	 */
	public static String getDateAfter(int day, String pattern) {
		Calendar now = Calendar.getInstance();
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		if (pattern == null || pattern.equals(""))
			pattern = "yyyy-MM-dd";
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(now.getTime());
	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDay(Date date) {
		String pattern = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDay(Date date, String pattern) {
		if (pattern == null || pattern.length() == 0)
			pattern = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	/**
	 * 格式化时间
	 * 
	 * @param formatTime
	 * @return
	 */
	public static String formatTime(String formatTime) {
		String pattern = "HH:mm:ss";
		SimpleDateFormat fmt = new SimpleDateFormat(pattern);
		return fmt.format(formatTime);
	}

	/**
	 * 格式化时间
	 * 
	 * @param formatTime
	 * @return
	 */
	public static String formatTime(Date date) {
		String pattern = "HH:mm:ss";
		SimpleDateFormat fmt = new SimpleDateFormat(pattern);
		return fmt.format(date);
	}

	/**
	 * 格式化时间
	 * 
	 * @param formatTime
	 * @return
	 */
	public static String formatTime(Date date, String pattern) {
		if (pattern == null || pattern.length() == 0)
			return formatTime(date);
		SimpleDateFormat fmt = new SimpleDateFormat(pattern);
		return fmt.format(date);
	}

	/**
	 * 将字符串按照格式转成date类型
	 * 
	 * @param strFormat
	 * @param dateValue
	 * @return
	 */
	public static Date StringToDate(String strFormat, String dateValue) {
		if (dateValue == null)
			return null;

		if (strFormat == null)
			strFormat = "yyyy-MM-dd";

		SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
		Date newDate = null;

		try {
			newDate = dateFormat.parse(dateValue);
		} catch (ParseException pe) {
			pe.printStackTrace();
			newDate = null;
		}

		return newDate;
	}

	public static Date getLast3Month() {
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, 3);
		Date otherDate = cal.getTime();
		// System.out.println( "today:   "+dateFormat.format(date)+
		// "   3   months   after:   "+dateFormat.format(otherDate));
		return otherDate;
	}

	/**
	 * 输入日期格式，是否早于时间
	 * 
	 * @param dateStr
	 * @return
	 */
	public static int compare_date(String DATE1, String DATE2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * 计算某日期未来第X天,然后转为String输出
	 * 
	 * @param pattern
	 *            ,显示日期格式
	 * @param dateNum
	 *            ,未来第X天数，int
	 * @return
	 */

	public static String formatFutureDay(Date date, String pattern, int dateNum) {
		if (pattern == null || pattern.length() == 0)
			pattern = "yyyy-MM-dd HH:mm:ss";
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, dateNum);
		date = c.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	/**
	 * 比较当前日期
	 * 
	 * @return java.util.date
	 */
	public static boolean compareDateByDate(Date date1, Date date2) {
		if (date1 == null) {
			return false;
		}
		if (date2 == null) {
			return true;
		}
		if (date1.getTime() >=date2.getTime()) {
			return true;
		}
		return false;
	}
	public static boolean compareDateByDate2(Date date1, Date date2) {
		if (date1 == null) {
			return false;
		}
		if (date2 == null) {
			return true;
		}
		if (date1.getTime() >date2.getTime()) {
			return true;
		}
		return false;
	}

	/**
	 * 获得当天的0时0分0秒的日期对象
	 * 
	 * @return
	 */
	public static Date getCurrentDateStartTime() {
		return getDateStartTime(new Date());
	}

	/**
	 * 获得指定日期的0时0分0秒的日期对象
	 * 
	 * @return
	 */
	public static Date getDateStartTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获得指定日期的yyyy-MM-dd的日期对象
	 * 
	 * @return
	 */
	public static Date getDates(Date date, String format) {
		Date dates = null;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			dates = sdf.parse(sdf.format(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return dates;
	}
	/**
	 * 获得格式化的时间间隔
	 * @param time　毫秒值
	 * @return　格式如0515 代表５小时１５分钟
	 */
	public static String formatHourMinute(long time){
		int h = (int)NumberUtils.div(time, 60*60*1000, 0);
		int m = (int)NumberUtils.div((time-h*60*60*1000),60*1000,0);
		h = h < 0 ? 0 : h;
		m = m < 0 ? 0 : m;
		return String.format("%s%s", h<10? "0"+h:h , m<10?"0"+m:m);
	}

	public static void main(String[] args) throws Exception {
		System.out.println(formatHourMinute(900000));
	}

}
